#include <Length.h>
#include <LengthUnit.h>

Length::Length(const unsigned int& amount, const LengthUnit& unit)
             :amountInBaseUnit(amount * unit.toAmountInBaseUnit( ))
{

}

bool Length::operator==(const Length & length) const
{
	return amountInBaseUnit == length.amountInBaseUnit;
}

bool Length::operator!=(const Length & length) const
{
	return !operator==(length);
}

Length Length::operator+(const Length & length) const
{
	return Length(amountInBaseUnit + length.amountInBaseUnit,FEET);
}

//Here toScreen( ) and toString( ) function need to be refactory!!
void Length::toScreen( ) const
{
	unsigned int quotient = 0;
	unsigned int rest = 0;
	quotient = amountInBaseUnit / MILE.toAmountInBaseUnit( );
	if(quotient > 0) 
		std::cout << quotient << " " << "MILE" << " ";
	rest = amountInBaseUnit % MILE.toAmountInBaseUnit( );
	if(rest == 0)
	{
		std::cout << std::endl;
		return;
	}
	quotient = rest / YARD.toAmountInBaseUnit( );
	if(quotient > 0)
		std::cout << quotient << " " << "YARD" << " ";
	rest = rest % YARD.toAmountInBaseUnit( );
	if (rest == 0)
	{
		std::cout << std::endl;
		return;
	}
	std::cout << rest << " " << "FEET" << std::endl;
}

void Length::toString(std::ostringstream& str) const
{
	unsigned int quotient = 0;
	unsigned int rest = 0;

	quotient = amountInBaseUnit / MILE.toAmountInBaseUnit( );
	if(quotient > 0) 
		str << quotient << " " << "MILE" << " ";
	rest = amountInBaseUnit % MILE.toAmountInBaseUnit( );
	if(rest == 0)
		return;
	quotient = rest / YARD.toAmountInBaseUnit( );
	if(quotient > 0)
		str  << quotient << " " << "YARD" << " ";
	rest = rest % YARD.toAmountInBaseUnit( );
	if (rest == 0)
		return;
	str  << rest << " " << "FEET";
}

void Length::toStringInBaseUnit(std::ostringstream& str) const
{
	str << amountInBaseUnit << " " << "FEET";
}
